package com.hyyt.shopjsp.tuan.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.tuan.vo.TuanProductVo;
import com.hyyt.shopjsp.tuan.pojo.TuanProductType;
import com.hyyt.shopjsp.tuan.service.ITuanProductService;
import com.hyyt.shopjsp.tuan.service.ITuanProductTypeService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/26 0026下午 6:48.
 */
@Controller
@RequestMapping("/back/tuanProductType")
public class TuanProductTypeController extends BaseController {

    private Logger log = Logger.getLogger(TuanProductTypeController.class);
    private String basePath = "/back/shop/tuanProductType/";

    @Resource
    private ITuanProductTypeService tuanProductTypeService;
    @Resource
    private ITuanProductService tuanProductService;

    /**
     * 进入团购分类列表页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoTuanProductTypePage")
    public String gotoTuanProductTypePage() {
        return basePath + "tuanProductTypeList";
    }

    /**
     * 根据分类查询商品
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoProductInfoListByTuanProductTypeId")
    public String gotoProductInfoListByTuanProductTypeId(Integer tuanProductTypeId,Map<String,Object> param ){
        param.put("tuanProductTypeId", tuanProductTypeId);
        return basePath+"tuanProductList";
    }

    /**
     * 查询团购分类集合
     * @param response
     */
    @RequestMapping(value = "findTuanList", method = RequestMethod.POST)
    @ResponseBody
    public void findTuanList(Page page,String sortName, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if(Utils.objectIsNotEmpty(sortName)){
           param.put("sortName",sortName);
        }
        param.put("sortColumns", "sortCode asc");
        int totalRecordCount = tuanProductTypeService.countTuanProductType(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<TuanProductType> list = tuanProductTypeService.queryTuanProductType(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 保存或者修改团购分类
     * @param tuanProductType
     * @return
     */
    @RequestMapping(value = "saveOrEditTuanProductType", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrEditTuanProductType(TuanProductType tuanProductType) {
        if(Utils.objectIsNotEmpty(tuanProductType)){
            if (Utils.objectIsEmpty(tuanProductType.getTuanProductTypeId())) {
                Timestamp createTime = new Timestamp(System.currentTimeMillis());
                tuanProductType.setCreateTime(createTime);
                tuanProductTypeService.saveTuanProductType(tuanProductType);
                return ResultMsg.create(true);
            }
            else {
                tuanProductTypeService.updateTuanProductType(tuanProductType);
                return ResultMsg.create(true);
            }
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 获得团购分类对象
     * @param tuanProductTypeId
     * @return
     */
    @RequestMapping(value = "getTuanProductTypeObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getTuanProductTypeObject(Integer tuanProductTypeId) {
        if (Utils.objectIsNotEmpty(tuanProductTypeId)) {
            TuanProductType tuanProductType = tuanProductTypeService.loadTuanProductType(tuanProductTypeId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(tuanProductType));
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 删除指定团购分类对象
     * @param tuanProductTypeId 删除团购分类的id，多个以 , 隔开
     * @return
     */
    @RequestMapping(value = "delTuanProductType", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg delTuanProductType(Integer tuanProductTypeId) {
        if(Utils.objectIsNotEmpty(tuanProductTypeId)){
            tuanProductTypeService.deleteTuanProductType(tuanProductTypeId);
           return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     *根据团购分类id查询商品
     * @param productFullName 商品名称
     * @param beginTime 开启时间
     * @param endTime 结束时间
     * @param page 分页
     * @param response 结果集
     * @param tuanProductTypeId 团购分类id
     */
    @RequestMapping(value = "listProductInfoByTuanProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void listProductInfoByTuanProductTypeId(String productFullName,String beginTime,String endTime,Page page, HttpServletResponse response, Integer tuanProductTypeId){
        Map<String, Object> params = new HashMap<>();
        if(productFullName!=null&&!"".equals(productFullName.trim())){
            params.put("productFullName",productFullName.trim());
        }
        if(Utils.objectIsNotEmpty(beginTime)){
            params.put("beginTime",beginTime.trim());
        }
        if(Utils.objectIsNotEmpty(endTime)){
            params.put("endTime",endTime.trim());
        }
        params.put("tuanProductTypeId",tuanProductTypeId);
        int totalRecordCount = tuanProductService.countTuanProduct(params);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<TuanProductVo> tuanProductList = tuanProductService.queryTuanProduct(params, page.getPageRecordBeginIndex(), page.getPageSize());
        params.clear();
        params.put("total", totalRecordCount);// total键 存放总记录数，必须的
        params.put("rows", tuanProductList);// rows键 存放每页记录 list

        ResultMsg.create(params, response);  //把内容发送到页面
    }
}